package view;

import control.MainPanelListener;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import net.coderazzi.filters.gui.AutoChoices;
import net.coderazzi.filters.gui.TableFilterHeader;

/**
 * The class is displaying a table of all registered cases which includes a filter function.
 *
 * @version 2014-05-25
 * @author Miljöpoliserna
 */
public class MainPanel extends JPanel
{

    private final JButton logoutButton;
    private final JButton newCaseButton;
    private final JTable table;
    private String[][] dataVal;
    private String[] cols;
    private final DefaultTableModel model;
    private MainPanelListener listener;
    private GridBagConstraints c;

    /**
     * Constructor of the class that sets all components in the panel.
     */
    public MainPanel()
    {

        this.logoutButton = new JButton("Avsluta");
        this.newCaseButton = new JButton("Registrera");

        logoutButton.setPreferredSize(new Dimension(94, 25));
        newCaseButton.setPreferredSize(new Dimension(94, 25));

        String[] cols = columnBase();

        model = new DefaultTableModel(dataVal, cols);
        table = new JTable(model)
        {
            @Override
            public boolean isCellEditable(int data, int cols)
            {
                return false;
            }

            @Override
            public Component prepareRenderer(TableCellRenderer r, int data, int cols)
            {
                Component c = super.prepareRenderer(r, data, cols);
                if (data % 2 == 0)
                {
                    c.setBackground(Color.WHITE);
                }
                else
                {
                    c.setBackground(Color.LIGHT_GRAY);
                }
                if (isCellSelected(data, cols))
                {
                    c.setBackground(Color.PINK);
                }
                return c;
            }
        };
        this.setBackground(Color.WHITE);
        table.setRowSelectionAllowed(true);
        TableFilterHeader filterHeader = new TableFilterHeader(table, AutoChoices.ENABLED);
        filterHeader.setBackground(Color.WHITE);

        table.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                if (e.getClickCount() == 2)
                {
                    JTable target = (JTable) e.getSource();
                    int row = target.getSelectedRow();
                    int column = target.getSelectedColumn();
                    // do some action
                }
            }
        });

        JScrollPane scrollPane = new JScrollPane(table);

        setLayout(new GridBagLayout());
        c = new GridBagConstraints();

        c.anchor = GridBagConstraints.NORTH;
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.insets = new Insets(50, 50, 0, 50);

        add(scrollPane, c);

        c.anchor = GridBagConstraints.SOUTHEAST;
        c.fill = GridBagConstraints.NONE;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 0.2;
        c.insets = new Insets(0, 0, 35, 50);

        add(logoutButton, c);

        c.anchor = GridBagConstraints.SOUTHEAST;
        c.fill = GridBagConstraints.NONE;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 0.2;
        c.insets = new Insets(0, 0, 35, 158);

        setVisible(true);

    }

    private String[] columnBase()
    {
        String[] coordinatorColumnNames =
        {
            "ID", "ÄRENDE", "ENHET", "RAPPORTDATUM", "STATUS", "HANDLÄGGARE", "TYP"
        };

        return coordinatorColumnNames;

    }

    /**
     * Adds a button to register new cases if the user is a coordinator.
     */
    public void addNewCaseButton()
    {
        add(newCaseButton, c);
    }

    /**
     * Creates a listener for the button creating a new case.
     *
     * @param listenForNewCaseButton
     */
    public void addListenerForNewCaseButton(ActionListener listenForNewCaseButton)
    {
        newCaseButton.addActionListener(listenForNewCaseButton);
    }

    /**
     * Creates a listener for the logout button.
     *
     * @param listenForLogoutButton
     */
    public void addListenerForLogutButton(ActionListener listenForLogoutButton)
    {
        logoutButton.addActionListener(listenForLogoutButton);
    }

    /**
     * Creates a listener for mouse action.
     *
     * @param mouseListener
     */
    public void addListenerForMouse(MouseListener mouseListener)
    {
        table.addMouseListener(mouseListener);
    }

    public JButton getLogoutButton()
    {
        return logoutButton;
    }

    public JButton getNewCaseButton()
    {
        return newCaseButton;
    }

    public JTable getTable()
    {
        return table;
    }

    public String[][] getDataVal()
    {
        return dataVal;
    }

    public void setTableData(String[][] rowData)
    {
        dataVal = rowData;
        model.setRowCount(0);
        for (String[] row : dataVal)
        {
            model.addRow(row);
        }

    }

    public void addTableRow(String[] row)
    {
        model.addRow(row);
    }

}
